<template>
  <!-- 模具任务表表单弹窗 -->
  <div>
    <el-dialog
      v-model="visibleDialog"
      :title="props.title"
      width="50%"
      draggable
      @close="closeDialog"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :inline="true"
        :rules="rules"
        label-width="100"
        v-if="!props.isReadonly"
      >
        <div>
          <el-row>
            <el-col :span="8">
              <el-form-item
                label="计划编号"
                prop="processingOrderNumber"
                style="width: 100%"
              >
                <el-input
                  v-if="!props.isReadonly"
                  v-model="formData.processingOrderNumber"
                  placeholder="请输入计划编号"
                  clearable
                />
                <span v-else>{{ formData.processingOrderNumber }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="工序"
                v-if="!props.isReadonly"
                prop="processesClassification"
                style="width: 100%"
              >
                <el-select
                  style="width: 100%"
                  v-if="!props.isReadonly"
                  v-model="formData.processesClassification"
                  placeholder="请选择工序"
                  clearable
                >
                  <el-option
                    v-for="item in dictionaryStore.filterDictList(
                      'ProcessType'
                    )"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
                <span v-else>
                  {{
                    dictionaryStore.filterDictList(
                      "ProcessType",
                      formData.processesClassification
                    )
                  }}
                </span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="产品图号"
                prop="figureNumber"
                style="width: 100%"
              >
                <el-input
                  v-if="!props.isReadonly"
                  v-model="formData.figureNumber"
                  placeholder="请输入产品图号"
                  clearable
                />
                <span v-else>{{ formData.figureNumber }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="成品物料编号"
                prop="materialCode"
                style="width: 100%"
              >
                <el-input
                  v-if="!props.isReadonly"
                  v-model="formData.materialCode"
                  placeholder="请输入成品物料编号"
                  clearable
                />
                <span v-else>{{ formData.materialCode }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="成品物料名"
                prop="materialName"
                style="width: 100%"
              >
                <el-input
                  v-if="!props.isReadonly"
                  v-model="formData.materialName"
                  placeholder="请输入成品物料名"
                  clearable
                />
                <span v-else>{{ formData.materialName }}</span>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="8">
              <el-form-item
                label="计划人员"
                prop="planUsers"
                style="width: 100%"
              >
                <el-input
                  v-if="!props.isReadonly"
                  v-model="formData.planUsers"
                  placeholder="请输入计划人员"
                  clearable
                />
                <span v-else>{{ formData.planUsers }}</span>
              </el-form-item>
            </el-col> -->
            <el-col :span="8">
              <el-form-item
                label="产品批号"
                prop="equipName"
                style="width: 100%"
              >
                <el-input
                  v-if="!props.isReadonly"
                  v-model="formData.equipName"
                  placeholder="请输入产品批号"
                  clearable
                />
                <span v-else>{{ formData.equipName }}</span>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item
                label="任务状态"
                prop="taskStatus"
                style="width: 100%"
              >
                <el-select
                  style="width: 100%"
                  v-if="!props.isReadonly"
                  v-model="formData.taskStatus"
                  clearable
                  placeholder="请选择任务状态"
                >
                  <el-option
                    v-for="item in dictionaryStore.filterDictList(
                      'completeStatus'
                    )"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
                <span v-else>{{
                  dictionaryStore.filterDictName(
                    "complete_status",
                    formData.taskStatus
                  )
                }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="计划数量"
                prop="planQuantity"
                style="width: 100%"
              >
                <el-input
                  v-if="!props.isReadonly"
                  v-model="formData.planQuantity"
                  placeholder="请输入计划数量"
                  clearable
                />
                <span v-else>{{ formData.planQuantity }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="计划模数"
                prop="mouldQuantity"
                style="width: 100%"
              >
                <el-input
                  v-if="!props.isReadonly"
                  v-model="formData.mouldQuantity"
                  placeholder="请输入计划模数"
                  clearable
                />
                <span v-else>{{ formData.mouldQuantity }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="生产日期"
                prop="planDate"
                style="width: 100%"
              >
                <el-date-picker
                  v-if="!props.isReadonly"
                  v-model="formData.planDate"
                  type="date"
                  placeholder="请选择生产日期"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                />
                <span v-else>{{ formData.planDate }}</span>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <!-- <el-card shadow="never" class="table-container">
          <div class="formTable">
            <el-table
              :data="formData.details"
              style="width: 100%"
              :key="Math.random"
              border
            >
              <el-table-column
                type="index"
                label="序号"
                align="center"
                width="100"
              />
              <el-table-column
                label="模具编号"
                props="mouldCode"
                min-width="150"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="'details.' + $index + '.mouldCode'"
                    :rules="rules.sonmouldCode"
                    label-width="0"
                  >
                    <el-input
                      v-if="!props.isReadonly"
                      v-model="row.mouldCode"
                      placeholder="请输入模具编号"
                      clearable
                    />
                    <span v-else>{{ row.mouldCode }}</span>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="模具名称"
                props="mouldName"
                min-width="150"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="'details.' + $index + '.mouldName'"
                    :rules="rules.sonmouldName"
                    label-width="0"
                  >
                    <el-input
                      v-if="!props.isReadonly"
                      v-model="row.mouldName"
                      placeholder="请输入模具名称"
                      clearable
                    />
                    <span v-else>{{ row.mouldName }}</span>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="模具类型"
                props="mouldType"
                min-width="150"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="'details.' + $index + '.mouldType'"
                    :rules="rules.sonmouldType"
                    label-width="0"
                  >
                    <el-input
                      v-if="!props.isReadonly"
                      v-model="row.mouldType"
                      placeholder="请输入模具类型"
                      clearable
                    />
                    <span v-else>{{ row.mouldType }}</span>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="模具位置id"
                props="locationId"
                min-width="150"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="'details.' + $index + '.locationId'"
                    :rules="rules.sonlocationId"
                    label-width="0"
                  >
                    <el-input
                      v-if="!props.isReadonly"
                      v-model="row.locationId"
                      placeholder="请输入模具位置id"
                      clearable
                    />
                    <span v-else>{{ row.locationId }}</span>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="模具位置名称"
                props="locationName"
                min-width="150"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="'details.' + $index + '.locationName'"
                    :rules="rules.sonlocationName"
                    label-width="0"
                  >
                    <el-input
                      v-if="!props.isReadonly"
                      v-model="row.locationName"
                      placeholder="请输入模具位置名称"
                      clearable
                    />
                    <span v-else>{{ row.locationName }}</span>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="出库状态"
                props="outboundStatus"
                min-width="150"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="'details.' + $index + '.outboundStatus'"
                    :rules="rules.sonoutboundStatus"
                    label-width="0"
                  >
                    <el-select
                      v-if="!props.isReadonly"
                      v-model="row.outboundStatus"
                      clearable
                      placeholder="请选择出库状态"
                    >
                      <el-option
                        v-for="item in dictionaryStore.filterDictList(
                          'outboundStatus'
                        )"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                    <span v-else>{{
                      dictionaryStore.filterDictName(
                        "outboundStatus",
                        row.outboundStatus
                      )
                    }}</span>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="合格状态"
                props="passStatus"
                min-width="150"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="'details.' + $index + '.passStatus'"
                    :rules="rules.sonpassStatus"
                    label-width="0"
                  >
                    <el-select
                      v-if="!props.isReadonly"
                      v-model="row.passStatus"
                      clearable
                      placeholder="请选择合格状态"
                    >
                      <el-option
                        v-for="item in dictionaryStore.filterDictList(
                          'pass_status'
                        )"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                    <span v-else>{{
                      dictionaryStore.filterDictName(
                        "pass_status",
                        row.passStatus
                      )
                    }}</span>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="扫码时间"
                props="scanTime"
                min-width="150"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="'details.' + $index + '.scanTime'"
                    :rules="rules.sonscanTime"
                    label-width="0"
                  >
                    <el-input
                      v-if="!props.isReadonly"
                      v-model="row.scanTime"
                      placeholder="请输入扫码时间"
                      clearable
                    />
                    <span v-else>{{ row.scanTime }}</span>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="出库时间"
                props="outboundTime"
                min-width="150"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="'details.' + $index + '.outboundTime'"
                    :rules="rules.sonoutboundTime"
                    label-width="0"
                  >
                    <el-input
                      v-if="!props.isReadonly"
                      v-model="row.outboundTime"
                      placeholder="请输入出库时间"
                      clearable
                    />
                    <span v-else>{{ row.outboundTime }}</span>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="创建人名"
                props="createdUserName"
                min-width="150"
              >
                <template #default="{ $index, row }">
                  <el-form-item
                    :prop="'details.' + $index + '.createdUserName'"
                    :rules="rules.soncreatedUserName"
                    label-width="0"
                  >
                    <el-input
                      v-if="!props.isReadonly"
                      v-model="row.createdUserName"
                      placeholder="请输入创建人名"
                      clearable
                    />
                    <span v-else>{{ row.createdUserName }}</span>
                  </el-form-item>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-card> -->
      </el-form>

      <el-descriptions
        v-if="props.isReadonly"
        border
        :column="3"
        v-show="props.isReadonly"
      >
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">计划编号</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.processingOrderNumber }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">产品图号</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.figureNumber }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">成品物料编号</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.materialCode }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">成品物料名</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.materialName }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">产品批号</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.equipName }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">生产日期</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.planDate }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">任务状态</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{
              dictionaryStore.filterDictName(
                "completeStatus",
                formData.taskStatus
              )
            }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">计划数量</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.planQuantity }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">计划模数</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.mouldQuantity }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">出库数</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.outboundQuantity }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">创建时间</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.createTime }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">创建人</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.createdUserName }}
          </div>
        </el-descriptions-item>
      </el-descriptions>
      <div class="formTable" v-if="props.isReadonly" style="margin-top: 10px">
        <el-table
          :data="formData.details"
          style="width: 100%"
          :key="Math.random"
          border
          :header-cell-style="{ textAlign: 'center' }"
        >
          <el-table-column
            type="index"
            label="序号"
            align="center"
            width="80"
          />
          <el-table-column label="模具编号" props="mouldCode" min-width="150">
            <template #default="{ $index, row }">
              <span>{{ row.mouldCode }}</span>
            </template>
          </el-table-column>
          <el-table-column label="模具名称" props="mouldName" min-width="150">
            <template #default="{ $index, row }">
              <span>{{ row.mouldName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="模具类型"
            props="mouldType"
            min-width="150"
            align="center"
          >
            <template #default="{ $index, row }">
              <span>{{ row.mouldType }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column
            label="模具位置id"
            props="locationId"
            min-width="150"
          >
            <template #default="{ $index, row }">
              <el-form-item
                :prop="'details.' + $index + '.locationId'"
                :rules="rules.sonlocationId"
                label-width="0"
              >
                <el-input
                  v-if="!props.isReadonly"
                  v-model="row.locationId"
                  placeholder="请输入模具位置id"
                  clearable
                />
                <span v-else>{{ row.locationId }}</span>
              </el-form-item>
            </template>
          </el-table-column> -->
          <el-table-column
            label="模具位置名称"
            props="locationName"
            min-width="150"
          >
            <!-- <template #default="{ $index, row }">
              <el-form-item
                :prop="'details.' + $index + '.locationName'"
                :rules="rules.sonlocationName"
                label-width="0"
              >
                <el-input
                  v-if="!props.isReadonly"
                  v-model="row.locationName"
                  placeholder="请输入模具位置名称"
                  clearable
                />
                <span v-else>{{ row.locationName }}</span>
              </el-form-item>
            </template> -->
            <template #default="{ $index, row }">
              <span>{{ row.locationName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="出库状态"
            props="outboundStatus"
            min-width="150"
            align="center"
          >
            <template #default="{ $index, row }">
              <!-- <el-form-item
                :prop="'details.' + $index + '.outboundStatus'"
                :rules="rules.sonoutboundStatus"
                label-width="0"
              >
                <el-select
                  v-if="!props.isReadonly"
                  v-model="row.outboundStatus"
                  clearable
                  placeholder="请选择出库状态"
                >
                  <el-option
                    v-for="item in dictionaryStore.filterDictList(
                      'outboundStatus'
                    )"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
                <span>{{
                  dictionaryStore.filterDictName(
                    "outboundStatus",
                    row.outboundStatus
                  )
                }}</span>
              </el-form-item> -->
              <span>{{
                dictionaryStore.filterDictName(
                  "outboundStatus",
                  row.outboundStatus
                )
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="合格状态"
            props="passStatus"
            min-width="100"
            align="center"
          >
            <template #default="{ $index, row }">
              <!-- <el-form-item
                :prop="'details.' + $index + '.passStatus'"
                :rules="rules.sonpassStatus"
                label-width="0"
              >
                <el-select
                  v-if="!props.isReadonly"
                  v-model="row.passStatus"
                  clearable
                  placeholder="请选择合格状态"
                >
                  <el-option
                    v-for="item in dictionaryStore.filterDictList('passStatus')"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
                <span v-else>{{
                  dictionaryStore.filterDictName("passStatus", row.passStatus)
                }}</span>
              </el-form-item> -->
              {{
                dictionaryStore.filterDictName(
                  "pass_status",
                  String(row.passStatus)
                )
              }}
            </template>
          </el-table-column>
          <el-table-column
            label="扫码时间"
            props="scanTime"
            min-width="180"
            align="center"
          >
            <!-- <template #default="{ $index, row }">
              <el-form-item
                :prop="'details.' + $index + '.scanTime'"
                :rules="rules.sonscanTime"
                label-width="0"
              >
                <el-input
                  v-if="!props.isReadonly"
                  v-model="row.scanTime"
                  placeholder="请输入扫码时间"
                  clearable
                />
                <span v-else>{{ row.scanTime }}</span>
              </el-form-item>
            </template> -->
            <template #default="{ $index, row }">
              <span>{{ row.scanTime }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="出库时间"
            props="outboundTime"
            min-width="180"
            align="center"
          >
            <!-- <template #default="{ $index, row }">
              <el-form-item
                :prop="'details.' + $index + '.outboundTime'"
                :rules="rules.sonoutboundTime"
                label-width="0"
              >
                <el-input
                  v-if="!props.isReadonly"
                  v-model="row.outboundTime"
                  placeholder="请输入出库时间"
                  clearable
                />
                <span v-else>{{ row.outboundTime }}</span>
              </el-form-item>
            </template> -->
            <template #default="{ $index, row }">
              <span>{{ row.outboundTime }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="创建人名"
            props="createdUserName"
            min-width="150"
            align="center"
          >
            <template #default="{ $index, row }">
              <span>{{ row.createdUserName }}</span>
            </template>
            <!-- <template #default="{ $index, row }">
              <el-form-item
                :prop="'details.' + $index + '.createdUserName'"
                :rules="rules.soncreatedUserName"
                label-width="0"
              >
                <el-input
                  v-if="!props.isReadonly"
                  v-model="row.createdUserName"
                  placeholder="请输入创建人名"
                  clearable
                />
                <span v-else>{{ row.createdUserName }}</span>
              </el-form-item>
            </template> -->
          </el-table-column>
        </el-table>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            v-if="!props.isReadonly"
            type="primary"
            :loading="loading"
            @click="handleSubmit"
            >确 定</el-button
          >
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "WMSEAMMOULDTASKINFOADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

/** 弹窗显示 **/
const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      // formRef.value.clearValidate();
    }, 50);
  }
);

/** formData定义表单数据 */
const formRef = ref(ElForm);
let formData = reactive({
  id: "",
  processesClassification: "",
  version: "",
  processingOrderNumber: "",
  figureNumber: "",
  materialCode: "",
  materialName: "",
  planUsers: "",
  equipName: "",
  planDate: "",
  taskStatus: "",
  planQuantity: "",
  mouldQuantity: "",
  outboundQuantity: "",
  passQuantity: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
  details: [
    {
      id: "",
      version: "",
      taskId: "",
      mouldCode: "",
      mouldName: "",
      mouldType: "",
      locationId: "",
      locationName: "",
      outboundStatus: "",
      passStatus: "",
      scanTime: "",
      outboundTime: "",
      remarks: "",
      createTime: "",
      createdUserId: "",
      createdUserName: "",
      lastUpdateTime: "",
      lastUpdatedUserId: "",
      lastUpdatedUserName: "",
      enabled: "",
      deleted: "",
    },
  ],
});

/** rules规则 */
const rules = reactive({
  processingOrderNumber: [
    { required: true, message: "请输入计划编号", trigger: "blur" },
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  processesClassification: [
    { required: true, message: "请输入工序编号", trigger: "blur" },
  ],
  figureNumber: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialCode: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialName: [
    { min: 1, max: 200, message: "长度在 1 到 200 个字符", trigger: "blur" },
  ],
  planUsers: [
    { min: 1, max: 200, message: "长度在 1 到 200 个字符", trigger: "blur" },
  ],
  equipName: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  planDate: [{ required: true, message: "请选择生产日期", trigger: "change" }],
  taskStatus: [],
  planQuantity: [],
  mouldQuantity: [],
  outboundQuantity: [],
  passQuantity: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  sontaskId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  sonmouldCode: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  sonmouldName: [
    { min: 1, max: 200, message: "长度在 1 到 200 个字符", trigger: "blur" },
  ],
  sonmouldType: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  sonlocationId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  sonlocationName: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  sonoutboundStatus: [],
  sonpassStatus: [],
  sonscanTime: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  sonoutboundTime: [],
  soncreateTime: [],
  soncreatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  soncreatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
});

const isValidated = ref(false);
/** 删除行 */
const handleDelete = (index: any) => {
  formData.details.splice(index, 1);
  isValidated.value && formRef.value.validate(() => {});
};

const details = {
  id: "",
  version: "",
  taskId: "",
  mouldCode: "",
  mouldName: "",
  mouldType: "",
  locationId: "",
  locationName: "",
  outboundStatus: "",
  passStatus: "",
  scanTime: "",
  outboundTime: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
};

/** 添加行 */
const handleAdd = () => {
  let sondata = JSON.parse(JSON.stringify(details));
  formData.details.push(sondata);
};

/** 编辑时赋值 */
watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      Object.assign(formData, props.data);
      if (formData.id) {
        formData.taskStatus = String(formData.taskStatus);
        formData.processesClassification = String(
          formData.processesClassification
        );

        getdetails();
      }
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  // formRef.value.resetFields();
  // formRef.value.clearValidate();
  formData.details = [JSON.parse(JSON.stringify(details))];
  formData.id = "";
  formData.version = "";
  formData.processingOrderNumber = "";
  formData.processesClassification = "";
  formData.figureNumber = "";
  formData.materialCode = "";
  formData.materialName = "";
  formData.planUsers = "";
  formData.equipName = "";
  formData.planDate = "";
  formData.taskStatus = "";
  formData.planQuantity = "";
  formData.mouldQuantity = "";
  formData.outboundQuantity = "";
  formData.passQuantity = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  // formData.lastUpdateTime = "";
  // formData.lastUpdatedUserId = "";
  // formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 模具任务表保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      console.log("formData", toRaw(formData));
      await request({
        url: "/eam/eammouldtaskinfo/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
      isValidated.value = false;
    } else {
      isValidated.value = true;
    }
  });
}

async function getdetails() {
  let sondetails = await request({
    url: "/eam/eammouldtaskinfo/getDetails",
    method: "post",
    data: formData,
  }).finally(() => {
    loading.value = false;
  });
  formData.details = sondetails.data;
}
</script>

<style lang="scss" scoped>
:deep(.el-descriptions__label.el-descriptions__cell.is-bordered-label) {
  width: 110px;
}
:deep(.el-descriptions__content.el-descriptions__cell.is-bordered-content) {
  min-width: 100px;
}
</style>
